#include "ObjectManager.h"
#include "IEntity.h"
CObjectManager* CObjectManager::s_Instance = nullptr;

CObjectManager* CObjectManager::GetInstance()
{
	if(s_Instance == nullptr)
		s_Instance = new CObjectManager;
	return s_Instance;
}

void CObjectManager::DeleteInstance()
{
	if(s_Instance != nullptr)
		delete s_Instance;
	s_Instance = nullptr;
}

void CObjectManager::AddObject(IEntity* ptr)
{
	m_lObjs.push_back(ptr);
	ptr->AddRef();
}

void CObjectManager::RemoveObject(IEntity* ptr)
{

	LOOIterator theit = m_lObjs.begin();
	while(theit != m_lObjs.end())
	{
		if((*theit) == ptr)
		{
			theit = m_lObjs.erase(theit);
			ptr->Release();
			break;
		}
		else
			++theit;
	}
}

void CObjectManager::RemoveEVERYTHING()
{
	for(LOOIterator theit = m_lObjs.begin(); theit != m_lObjs.end(); ++theit)
	{
		(*theit)->Release();
	}
	m_lObjs.clear();
}

void CObjectManager::UpdateEVERYTHING(float f_ElapsedTime)
{
	for(LOOIterator theit = m_lObjs.begin(); theit != m_lObjs.end(); ++theit)
		(*theit)->Update(f_ElapsedTime);
}

void CObjectManager::CheckCollisions()
{
	for(LOOIterator theitI = m_lObjs.begin(); theitI != m_lObjs.end(); ++theitI)
	{
		for(LOOIterator theitK = m_lObjs.begin(); theitK != m_lObjs.end(); ++theitK)
		{
			if((*theitI)->GetType() != (*theitK)->GetType())
			{
				(*theitI)->CheckCollision(*theitK);
			}
		}
	}
}

void CObjectManager::RenderEVERYTHING()
{
	for(LOOIterator theit = m_lObjs.begin(); theit != m_lObjs.end(); ++theit)
		(*theit)->Render();
}